package com.zizy.healthexpert.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.zizy.healthexpert.Adapter.listSportsAdapter;
import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.MyDatabase_SportsRecords;
import com.zizy.healthexpert.Practical.Practical;
import com.zizy.healthexpert.R;

import java.util.ArrayList;

public class RecordSportsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_sports);
        bindAdapter();
    }
    public void bindAdapter(){
        ListView listSports = (ListView) findViewById(R.id.listSports);
        MyDatabase_SportsRecords myDatabase_sportsRecords = new MyDatabase_SportsRecords(getApplicationContext());
        ArrayList<Object> sportsRecordsListObj=myDatabase_sportsRecords.SearchDatabase("*");
        myDatabase_sportsRecords.close();
        listSportsAdapter sportsAdapter = new listSportsAdapter(getApplicationContext(), R.layout.list_record_sports, sportsRecordsListObj);
        listSports.setAdapter(sportsAdapter);

    }
    public void btnAddSportsClick(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("增加运动");
        //    通过LayoutInflater来加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(this).inflate(R.layout.dia_pick_sports, null);
        //    设置我们自己定义的布局文件作为弹出框的Content
        builder.setView(view);

        final Spinner spnSports = (Spinner)view.findViewById(R.id.spnSports);
        final TextView txtAddSportsTime = (TextView)view.findViewById(R.id.txtAddSportsTime);
        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,new String[]{"慢跑","快跑","中速跑","游泳","爬楼梯","爬山","球类运动"});
        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnSports.setAdapter(adapter);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                MyDatabase_SportsRecords myDatabase_sportsRecords = new MyDatabase_SportsRecords(getApplicationContext());
                myDatabase_sportsRecords.insertDatabase(Config.userID, Practical.getTimeFormat(),(String)spnSports.getSelectedItem(),txtAddSportsTime.getText()+"");
                myDatabase_sportsRecords.close();
                Toast.makeText(getApplicationContext(),"新增记录成功",Toast.LENGTH_SHORT).show();
                bindAdapter();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        builder.show();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.record_sports, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
